/*
 * BasicTazioNodeID.java.java    0.0.1    Oct 24, 2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.core.identifiers;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * This class implements the {@link TazioNodeID} interface, using the first
 * {@link java.net.InetAddress} it founds on the first {@link NetworkInterface}
 * (preferebly not a loopback one) as the node's identification string.
 *  
 * @version 0.0.1 Oct 24, 2009
 * @author Andrea Sorbini <as@mentalsmash.org>
 *
 */
public class BasicTazioNodeID implements TazioNodeID {

	private static final long serialVersionUID = -4105139012947608515L;

	private final InetAddress address;

	private final String symbolicName;
	
	public BasicTazioNodeID(String symbolicaName) {
		NetworkInterface netInt = null;

		this.symbolicName = symbolicaName;
		
		try {
			for (Enumeration<NetworkInterface> enumNI = NetworkInterface
					.getNetworkInterfaces(); enumNI.hasMoreElements();) {
				NetworkInterface ni = enumNI.nextElement();

				if (!ni.isLoopback()) {
					netInt = ni;
					break;
				}
			}
			
			/*
			 * No non-loopback interfaces were found, so
			 * we just take the first one.
			 */
			if (netInt == null) {
				netInt = NetworkInterface.getNetworkInterfaces().nextElement();
			}
			
			this.address = netInt.getInetAddresses().nextElement();
		} catch (SocketException e) {
			throw new RuntimeException("Unable to retrieve network interface's address");
		}

	}

	@Override
	public String getValue() {
		String value = ""+this.symbolicName+"@"+this.address.getHostAddress();
		return value;
	}
	
	@Override
	public InetAddress getAddress(){
		return this.address;
	}
	
	@Override
	public String getSymbolicName(){
		return this.symbolicName;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BasicTazioNodeID other = (BasicTazioNodeID) obj;
		if (address == null) {
			if (other.address != null){
				return false;
			}
		} else if (!address.equals(other.address)){
			return false;
		}
		if (symbolicName != other.getSymbolicName()){
			return false;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "BasicTazioNodeID [" + getValue() + "]";
	}
	
}
